let dto = (sub) => {
    let template;
    template = `
        import { Rule, RuleType } from '@midwayjs/validate';
        import { PageDTO } from '@dto/common/page.dto';
        
        // 查询参数
        export class ListModelDTO extends PageDTO {
          ${CreateModelDTO(sub, false)}
        }
        
        // 新增参数
        export class CreateModelDTO {
          ${CreateModelDTO(sub)}
        }
        
        // 修改参数
        export class UpdateModelDTO extends CreateModelDTO {
          @Rule(RuleType.number().required())
          id: number;
        }
    `;

    return template
}

function CreateModelDTO(sub, required = true) {
    let template = ``
    const typeObj = {
        varchar: ['string', 'string'],
        int: ['number', 'number'],
        datetime: ['date', 'string'],
    }
    for(let i of sub) {
        if(i['驼峰'] === 'id') continue;
        template += `
            // ${i.comment}
            @Rule(RuleType.${typeObj[i.type][0]}()${isRequired(i, required)})
            ${i['驼峰']}?: ${typeObj[i.type][1]};
        `
    }
    return template
}

function isRequired(data, required) {
    if(!required) return ''
    if(required && data.isRequired) {
        return '.required()'
    }else {
        return ''
    }
}


export default dto;
